#include<iostream>
#include<vector>
#include<stack>

using namespace std;

/**/

vector<int> nextGreaterElements(vector<int>& nums) {
    int n = nums.size();
    vector<int> ans(n, -1);
    stack<int> st;
    for (int i = 0; i < n * 2 - 1; i++) {
        while (!st.empty() && nums[st.top()] < nums[i % n]) {
            ans[st.top()] = nums[i % n];
            st.pop();
        }
        st.push(i % n);
    }
    return ans;
}

int main()
{
    vector<int> arr = { 1,2,3,4,3 };
    vector<int> ret = nextGreaterElements(arr);
    for (auto x : ret)
    {
        cout << x << " ";
    }
    return 0;
}